package org.ee.rabbitmq.listener;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.rabbit.annotation.RabbitListener;

/**
 *
 * @Author zlj
 * @Date 2025/11/10 22:57
 */
public abstract class RabbitMessageListener<T> {
    private final Class<T> messageType;
    private final ObjectMapper objectMapper = new ObjectMapper();

    protected RabbitMessageListener(Class<T> messageType) {
        this.messageType = messageType;
    }

    @RabbitListener(queues = "#{__listener.queueName}")
    public void onMessage(String json) throws Exception {
        T msg = objectMapper.readValue(json, messageType);
        handleMessage(msg);
    }

    public abstract void handleMessage(T message);

    // 由子类指定队列名
    public abstract String getQueueName();
}
